package com.itheima.web;

import com.github.pagehelper.PageInfo;
import com.itheima.Service.UserService;
import com.itheima.dao.pojo.User;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet("/user")
public class UserServlet extends BaseServlet {

//    实现新的分页查询
    public void newPageMethod(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得请求中的数据(当前页码)
        String currentPage =request.getParameter("currentPage") ;
        int curPage = 1;
        if (currentPage != null) {
            curPage = Integer.parseInt(currentPage);
        }
        //创建对象调用方法
        UserService userService = new UserService();
        PageInfo<User> pageInfo = userService.getUserFY(curPage);
        //将数据都传递到域中
        request.setAttribute("pageInfo",pageInfo);
        //跳转页面
        request.getRequestDispatcher("/pageList.jsp").forward(request,response);

    }




//    ---------------------------------------------------------------------

    //实现搜索框
    public void getUserByKeyword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取浏览器传递过来的值
        String keyword1 = request.getParameter("keyword");
        String currentPage = request.getParameter("currentPage");
        //调用service中的方法
        int curPage = 1;
        if (currentPage != null) {
            curPage = Integer.parseInt(currentPage);
        }

        
        UserService userService = new UserService();
        PageInfo<User> pageInfo = userService.getUserByKeyWord(curPage, keyword1);

        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("keyword",keyword1);

        request.getRequestDispatcher("/pageList.jsp").forward(request,response);
    }



//    ---------------------------------------------------------------------------
    //page 分页查询(简单)
    public void pageMethod(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得请求中的数据
        Integer currentPage =Integer.parseInt(request.getParameter("currentPage")) ;
        //创建对象调用方法
        UserService userService = new UserService();
        //获取每页的人数
        ArrayList<User> users = userService.pagingUser(currentPage);
        //获取最大的个数
        Integer maxItem = userService.getMax();
        //获取需要分的页数
        Integer maxPage = (maxItem/5)+1;
        //将数据都传递到域中
        request.setAttribute("currentPage",currentPage);
        request.setAttribute("users",users);
        request.setAttribute("maxPage",maxPage);
        //跳转页面
        request.getRequestDispatcher("/pageList.jsp").forward(request,response);
    }
//----------------------------------------------------------------------
    //修改用户根据id
    public void updateUserById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取传递过来的数据
        Map<String, String[]> parameterMap = request.getParameterMap();
        //创建用户对象
        User user = new User();
        try {
            BeanUtils.populate(user, parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //调用Service中的修改方法
        UserService userService = new UserService();
        boolean b = userService.setUserById(user);
        //根据返回值跳转页面
        if (b) {
            //表示正确 跳转到正确的页面
            request.getRequestDispatcher("update_ok.jsp").forward(request, response);
        } else {
            //表示错误 跳转到错误的页面
            request.getRequestDispatcher("update_sb.jsp").forward(request, response);
        }
    }


    //根据id获取用户
    public void queryUserById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求中的id值
        Integer id =Integer.parseInt(request.getParameter("id")) ;
        //调用方法根据id获取用户的信息
        UserService userService = new UserService();
        User user = userService.getUserById(id);
        //将suer对象转发
        request.setAttribute("user" , user);
        request.getRequestDispatcher("/update.jsp").forward(request,response);
    }


//--------------------------------------------------
    //删除
    public void deleteUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取传递过来的数据
        Map<String, String[]> map = request.getParameterMap();
        //创建一个user对象
        User user = new User();
        //调用方法
        UserService userService = new UserService();
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        boolean b = userService.deleteUser(user);
        if (b){
            request.getRequestDispatcher("delete_ok.jsp").forward(request,response);
        }else {
            request.getRequestDispatcher("delete_sb.jsp").forward(request,response);
        }
    }




//----------------------------------------------------------------------------
    //添加用户
    public void addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取浏览器传递的数据 用map集合的方式
        Map<String, String[]> parameterMap = request.getParameterMap();
        //擦混稿件用户对象
        User user1 = new User();
        //调用方法将集合封装到对象各中
        try {
            BeanUtils.populate(user1,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        //Service创建对象调用添加用户的方法
        UserService userService = new UserService();
        boolean b = userService.addUser(user1);
        //判断返回值
        if (b){
            //如果b==true 表示插入成功 跳转到成功的页面
            request.getRequestDispatcher("add_ok.jsp").forward(request,response);
        }else {
            //表示没插入成功 跳转到添加失败的页面
            request.getRequestDispatcher("add_sb.jsp").forward(request,response);
        }
    }





//    ----------------------------------------------------------------------------------
    //查询所有用户
    public void queryAllUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //调用Service中的cx类中的方法查询用户
        UserService userService = new UserService();

        //获取结果list集合
        List<User> user = userService.getUser();
        //将list集合传到session域中
        request.getSession().setAttribute("UserList", user);
        //转发到list.jsp(查询)的页面
        request.getRequestDispatcher("list.jsp").forward(request, response);
    }
}
